package com.cn.yz.springboot.rabbitmq;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.connection.Connection;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.cn.yz.springboot.model.RabbitMQBind;
import com.cn.yz.springboot.model.RabbitMQExchange;
import com.cn.yz.springboot.model.RabbitMQQueue;
import com.cn.yz.springboot.service.RabbitMQBindService;
import com.cn.yz.springboot.service.RabbitMQExchangeService;
import com.cn.yz.springboot.service.RabbitMQQueueService;
import com.rabbitmq.client.Channel;

/***
 * 通用性配置
 * @author king
 *
 */
@Configuration
public class AmqpConfig {
	
	private static final Logger logger = LoggerFactory.getLogger(AmqpConfig.class);
	
//	public static final String QUEUE_NAME = "spring-boot";
//	public static final String SMS_QUEUE_NAME = "sms-spring-boot";
//    public static final String QUEUE_EXCHANGE_NAME = "spring-boot-exchange";
//    /**
//     * type：有direct、fanout、topic三种
//     * direct
//     */
//    public static final String exchange_type = "direct";//type：有direct、fanout、topic三种
//    public static final String routingKey = "routingKey";
//    public static final String routingKey_1 = "routingKey_1";

    @Autowired
    private RabbitMQBindService rabbitMQBindService;
    
    @Autowired
    private RabbitMQExchangeService rabbitMQExchangeService;
    
    @Autowired
    private RabbitMQQueueService rabbitMQQueueService;
    
	@Bean
	public Channel channel(ConnectionFactory connectionFactory) throws IOException, Exception{
		Connection connection = connectionFactory.createConnection();
		Channel channel  = connection.createChannel(true);
		
		//设置queue
		//logger.info("===========================queue==================================");
		Map<Integer,RabbitMQQueue> queuesmap = new HashMap<Integer,RabbitMQQueue>();
		List<RabbitMQQueue> queuelist = rabbitMQQueueService.findAllForEffective();
		if(!queuelist.isEmpty()){
			for(int i = 0 ; i < queuelist.size(); i++){
				RabbitMQQueue queue = queuelist.get(i);
				//channel.queueDelete(queue.getQueueName());
				channel.queueDeclare(queue.getQueueName(), booleanFromInteger(queue.getDurable()), 
						booleanFromInteger(queue.getExclusive()), 
						booleanFromInteger(queue.getAutodelete()), null);
				queuesmap.put(queue.getId(), queue);
			}
		}
		
		//设置exchange
		//logger.info("===========================exchange==================================");
		Map <Integer,RabbitMQExchange> exchangemap = new HashMap<Integer,RabbitMQExchange>();
		List<RabbitMQExchange> exchangelist= rabbitMQExchangeService.findAllForEffective();
		if(!exchangelist.isEmpty()){
			for(int i = 0; i < exchangelist.size(); i++){
				RabbitMQExchange exchange = exchangelist.get(i);
				channel.exchangeDeclare(exchange.getExchangeName(), exchange.getExchangeType(), 
						booleanFromInteger(exchange.getDurable()), booleanFromInteger(exchange.getAutodelete()), 
						booleanFromInteger(exchange.getInternal()), null);
				exchangemap.put(exchange.getId(), exchange);
			}
		}
		
		//绑定
		List<RabbitMQBind> rabbitMQBinds = rabbitMQBindService.findAllForEffective();
		if(!rabbitMQBinds.isEmpty()){
			for(int i=0;i<rabbitMQBinds.size();i++){
				RabbitMQBind rabbitMQBind = rabbitMQBinds.get(i);
				if(queuesmap.get(rabbitMQBind.getQueueId())==null){
					throw new Exception("第"+rabbitMQBind.getId()+"条bind记录，没有找到第"+rabbitMQBind.getQueueId()+"条queue");
				}
				if(exchangemap.get(rabbitMQBind.getExchangeId())==null){
					throw new Exception("第"+rabbitMQBind.getId()+"条bind记录，没有找到第"+rabbitMQBind.getQueueId()+"条exchange");
				}
				channel.queueBind(queuesmap.get(rabbitMQBind.getQueueId()).getQueueName(), 
						exchangemap.get(rabbitMQBind.getExchangeId()).getExchangeName(), 
						rabbitMQBind.getRoutingKey(), null);		
			}
		}
		return channel;
	}
	 
	private boolean booleanFromInteger(Integer obj){
		if(obj!=null&&obj==1){
			return true;
		} else {
			return false;
		}
	}
}
